/*
 * ******************************************************************************
 *   Copyright (c) 2013-2014 Gabriele Mariotti.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *  *****************************************************************************
 */

package it.gmariotti.cardslib.demo.fragment.nativeview;

import it.gmariotti.cardslib.demo.ResourceTable;
import it.gmariotti.cardslib.demo.cards.ColorCard;
import it.gmariotti.cardslib.demo.fragment.v1.MaterialV1Fragment;
import it.gmariotti.cardslib.library.Constants;
import it.gmariotti.cardslib.library.internal.Card;
import it.gmariotti.cardslib.library.internal.CardHeader;
import it.gmariotti.cardslib.library.internal.CardThumbnail;
import it.gmariotti.cardslib.library.view.CardView;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.ScrollView;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.element.StateElement;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;

/**
 * Refresh/Replace card value example
 *
 * @author Gabriele Mariotti (gabri.mariotti@gmail.com)
 */
public class NativeChangeValueCardFragment extends MaterialV1Fragment {

    protected ScrollView mScrollView;
    private CardExample card1;
    private CardExample2 card2;
    private CardExample3 card3;
    private ColorCard card4;

    private CardView cardView1;
    private CardView cardView2;
    private CardView cardView3;
    private CardView cardView4;

    @Override
    protected int getSubTitleHeaderResourceId() {
        return ResourceTable.String_header_title_subtitle_ex_refresh;
    }

    @Override
    protected int getTitleHeaderResourceId() {
        return ResourceTable.String_header_title_group2;
    }

    @Override
    protected String getDocUrl() {
        return "";
    }

    @Override
    protected String getSourceUrl() {
        return "";
    }


    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_demo_fragment_native_changevalue);

        mScrollView = (ScrollView) findComponentById(ResourceTable.Id_card_scrollview);
        findComponentById(ResourceTable.Id_refresh).setClickedListener(component -> {
            changeCard1();
            changeCard2();
            changeCard3();
            changeCard4();
        });
        initCards();
    }


    /**
     * Inits the initial card
     */
    private void initCards() {

        card1 = new CardExample(this, "Header", "Title");
        cardView1 = (CardView) this.findComponentById(ResourceTable.Id_carddemo_card_changevalue_id);
        cardView1.setCard(card1);

        card2 = new CardExample2(this, "Header", "Title");
        cardView2 = (CardView) this.findComponentById(ResourceTable.Id_carddemo_card_changevalue_id2);
        cardView2.setCard(card2);

        card3 = new CardExample3(this, "Header", "Title");
        cardView3 = (CardView) this.findComponentById(ResourceTable.Id_carddemo_card_changevalue_id3);
        cardView3.setCard(card3);

        try {
            card4 = new ColorCard(this);
            card4.setTitle("A simple car");
            cardView4 = (CardView) this.findComponentById(ResourceTable.Id_carddemo_card_changevalue_id4);
            StateElement stateElement = new StateElement();
            RgbColor rgbColor1 = new RgbColor(getResourceManager().getElement(ResourceTable.Color_demo_card_background_color3).getColor());
            ShapeElement shapeElement1 = new ShapeElement();
            shapeElement1.setRgbColor(rgbColor1);
            stateElement.addState(new int[]{}, shapeElement1);

            RgbColor rgbColor2 = new RgbColor(getResourceManager().getElement(ResourceTable.Color_demo_card_background_color1).getColor());
            ShapeElement shapeElement2 = new ShapeElement();
            shapeElement1.setRgbColor(rgbColor2);
            stateElement.addState(new int[]{}, shapeElement2);
            card4.setBackgroundResource(stateElement);
            cardView4.setCard(card4);
        } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }
    }


    public class CardExample extends Card {

        protected String mTitleHeader;
        protected String mTitleMain;

        public CardExample(Context context, String titleHeader, String titleMain) {
            super(context, ResourceTable.Layout_carddemo_example_inner_content);
            this.mTitleHeader = titleHeader;
            this.mTitleMain = titleMain;
            init();
        }

        private void init() {

            //Create a CardHeader
            CardHeader header = new CardHeader(getContext());

            //Set the header title
            header.setTitle(mTitleHeader);

            //Add a popup menu. This method set OverFlow button to visible
            header.setPopupMenu(ResourceTable.Layout_menu_item_layout, (CardHeader.OnClickCardHeaderPopupMenuListener) (card, item) -> new ToastDialog(getContext()).setText("Click on card menu" + mTitleHeader + " item=" + item.getTitle()).show());
            addCardHeader(header);

            //Add ClickListener
            setClickedListener((card, view) -> new ToastDialog(getContext()).setText("Click Listener card=" + mTitleHeader).show());

            //Set the card inner text
            setTitle(mTitleMain);
        }

    }


    public class CardExample2 extends Card {

        protected String mTitleHeader;
        protected String mTitleMain;

        public CardExample2(Context context, String titleHeader, String titleMain) {
            super(context);
            this.mTitleHeader = titleHeader;
            this.mTitleMain = titleMain;
            init();
        }

        private void init() {

            //Create a CardHeader
            CardHeader header = new CardHeader(getContext());

            //Set the header title
            header.setTitle(mTitleHeader);
            addCardHeader(header);

            CardThumbnail thumb = new CardThumbnail(getContext());
            thumb.setDrawableResource(ResourceTable.Media_ic_launcher);
            addCardThumbnail(thumb);

            //Add ClickListener
            setClickedListener((card, view) -> new ToastDialog(getContext()).setText("Click Listener card=" + mTitleHeader).show());

            //Set the card inner text
            setTitle(mTitleMain);
        }

    }

    /**
     * Changes same values
     */
    private void changeCard1() {

        //Change Header
        card1.getCardHeader().setTitle("New header");

        //Change main
        card1.setTitle("New title");

        //Remove click listener
        card1.setClickedListener(null);
        card1.setClickable(false);

        //Remove shadow
        card1.setShadow(false);

        //Call refresh
        //cardView1.refreshCard(card1);
        card1.notifyDataSetChanged();

    }


    /**
     * Changes same values
     */
    private void changeCard2() {

        //Change Header
        card2.getCardHeader().setTitle("New image");

        card2.getCardThumbnail().setDrawableResource(ResourceTable.Media_ic_std_launcher);

        //Call refresh
        //cardView2.refreshCard(card2);
        card2.notifyDataSetChanged();
    }

    /**
     * Replaces inner layout
     */
    private void changeCard3() {

        card3.setInnerLayout(ResourceTable.Layout_carddemo_suggested_inner_content);
        card3.force = true;
        cardView3.replaceCard(card3);
    }

    /**
     * Change background dinamically
     */
    private void changeCard4() {
        try {
            StateElement stateElement = new StateElement();
            RgbColor rgbColor1 = new RgbColor(getResourceManager().getElement(ResourceTable.Color_demo_card_background_color3).getColor());
            ShapeElement shapeElement1 = new ShapeElement();
            shapeElement1.setRgbColor(rgbColor1);
            stateElement.addState(new int[]{}, shapeElement1);

            RgbColor rgbColor2 = new RgbColor(getResourceManager().getElement(ResourceTable.Color_demo_card_background_color2).getColor());
            ShapeElement shapeElement2 = new ShapeElement();
            shapeElement1.setRgbColor(rgbColor2);
            stateElement.addState(new int[]{}, shapeElement2);
        } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }

    }

    public class CardExample3 extends Card {

        protected String mTitleHeader;
        protected String mTitleMain;
        protected boolean force = false;

        public CardExample3(Context context, String titleHeader, String titleMain) {
            super(context);
            this.mTitleHeader = titleHeader;
            this.mTitleMain = titleMain;
            init();
        }

        private void init() {

            //Create a CardHeader
            CardHeader header = new CardHeader(getContext());

            //Set the header title
            header.setTitle(mTitleHeader);
            addCardHeader(header);

            CardThumbnail thumb = new CardThumbnail(getContext());
            thumb.setDrawableResource(ResourceTable.Media_ic_launcher);
            addCardThumbnail(thumb);

            //Set the card inner text
            setTitle(mTitleMain);
        }

        @Override
        public void setupInnerViewElements(ComponentContainer parent, Component view) {

            if (force) {
                Text title = (Text) view.findComponentById(ResourceTable.Id_carddemo_suggested_title);
                Text member = (Text) view.findComponentById(ResourceTable.Id_carddemo_suggested_memeber);
                Text subtitle = (Text) view.findComponentById(ResourceTable.Id_carddemo_suggested_subtitle);
                Text community = (Text) view.findComponentById(ResourceTable.Id_carddemo_suggested_community);
                if (title != null && member != null && subtitle != null && community != null) {
                    if (title != null)
                        title.setText(ResourceTable.String_demo_suggested_title);

                    if (member != null)
                        member.setText(ResourceTable.String_demo_suggested_member);

                    if (subtitle != null)
                        subtitle.setText(ResourceTable.String_demo_suggested_subtitle);

                    if (community != null)
                        community.setText(ResourceTable.String_demo_suggested_community);
                }
            } else {
                super.setupInnerViewElements(parent, view);
            }
        }

    }

}
